10 ' magicsq5.bas - FreeWare 2005 - eric tchong
20 GOTO 60 ' begin
30 SAVE"magicsq5",A:LIST-60
40 GOTO 340 ' clear line
50 GOTO 360 ' get key
60 CLS:KEY OFF:DEFDBL E,M,N,S:DIM MN(25), SN(25):DEFSTR Q:Q=MKI$(0)
70 FOR X=1 TO 25:READ MN(X):NEXT
80 PRINT "This program generates a 5x5 Magic Square"
90 PRINT "Enter a number divisible by 5":PRINT
100 PRINT "Save results in an ascii diskfile <y/n> ?"
110 GOSUB 50:CLS
120 IF ASC(Q)=89 OR ASC(Q)=121 THEN COPY=1 ELSE COPY=0:GOTO 150
130 LINE INPUT "Enter a DOS filename ? ";Z$
140 OPEN "O",#1,Z$:CLS
150 INPUT "Number 65 to 99999995 [0=exit] ";N
160 IF N=0 THEN CLS:END
170 IF N/5<>INT(N/5) OR N<65 OR N>99999995# THEN GOSUB 40:GOTO 150
180 GOSUB 40:N$=STR$(N):L=LEN(N$)
190 M=N/5:E=M-13:PRINT
200 FOR X=1 TO 25:SN(X)=MN(X)+E:NEXT
210 PRINT "Magic square of";N:PRINT
220 IF COPY THEN PRINT #1,"Magic square of";N:PRINT #1,""
230 FOR X=1 TO 25
240  IF L=2 THEN PRINT USING "###";SN(X);:IF COPY THEN PRINT #1,USING "###";SN(X);:GOTO 310 ELSE 310
250  IF L=3 THEN PRINT USING "####";SN(X);:IF COPY THEN PRINT #1,USING "####";SN(X);:GOTO 310 ELSE 310
260  IF L=4 THEN PRINT USING "#####";SN(X);:IF COPY THEN PRINT #1,USING "#####";SN(X);:GOTO 310 ELSE 310
270  IF L=5 THEN PRINT USING "######";SN(X);:IF COPY THEN PRINT #1,USING "######";SN(X);:GOTO 310 ELSE 310
280  IF L=6 THEN PRINT USING "#######";SN(X);:IF COPY THEN PRINT #1,USING "#######";SN(X);:GOTO 310 ELSE 310
290  IF L=7 THEN PRINT USING "########";SN(X);:IF COPY THEN PRINT #1,USING "########";SN(X);:GOTO 310 ELSE 310
300  PRINT USING "#########";SN(X);:IF COPY THEN PRINT #1,USING "#########";SN(X);
310  IF X/5=INT(X/5) THEN PRINT:PRINT:IF COPY THEN PRINT #1,"":PRINT #1,""
320 NEXT:GOTO 150
330 ' clear line
340 PRINT CHR$(30);:PRINT STRING$(79,32):PRINT CHR$(30);:RETURN
350 ' get key
360 LSET Q=MKI$(0)
370 WHILE CVI(Q)=0:MID$(Q,1)=INKEY$:WEND:RETURN
380 ' 5x5 data
390 DATA  3, 16,  9, 22, 15
400 DATA 20,  8, 21, 14,  2
410 DATA  7, 25, 13,  1, 19
420 DATA 24, 12,  5, 18,  6
430 DATA 11,  4, 17, 10, 23
